<?php

/**
 * Namodg - Ajax Forms Generator
 *
 * @desc Namodg allows developers to make ajax-driven forms easily. It uses OOP aproach,
 *       which means developers has to write less code!
 * @author Maher Salam <admin@namodg.com>
 * @link http://namodg.com
 * @copyright Copyright (c) 2010-2011, Maher Salam
 *
 * Dual licensed under the MIT and GPL licenses:
 *   @license http://www.opensource.org/licenses/mit-license.php
 *   @license http://www.gnu.org/licenses/gpl.html
 */

/*
 * Include dependencies
 */
require_once 'includes/config.php';
require_once 'includes/classes/namodg.class.php';
require_once 'includes/classes/rain.tpl.class.php';

/*
 * Initialize a new Namodg object
 */
$form = new Namodg($config);

/**
 * Check for ajax request to get the phrases used in the runtime by Javascript
 */
if ( $form->isAjaxRequest() && $_POST['getPhrases']) {
    echo json_encode( $form->getJsPhrases() );
    exit;
}

/**
 * Check the data passed to this file. If it's not valid and can't be processed,
 * return the user to the index
 */
if ( ! $form->canBeProcessed() ) {
    header('Location: index.php');
    exit;
}

/**
 * Initialize a new RainTPL object, for templating
 *
 * @link http://www.raintpl.com/
 */
$tpl = new RainTPL();

/**
 * Configure RainTPL
 *
 * @see http://www.raintpl.com/Documentation/Documentation-for-PHP-developers/Methods/Configure/
 */
RainTPL::configure('tpl_dir', 'templates/air/');
RainTPL::configure('cache_dir', 'cache/');

/**
 * Assign some variables
 *
 * @see http://www.raintpl.com/Documentation/Documentation-for-PHP-developers/Methods/Assign/
 */
$tpl->assign('form_title', $config['form_title']);
$tpl->assign('version', Namodg::version);

/**
 * Define a function to show a sending fail message
 *
 * @global Namodg $form
 * @global RainTPL $tpl
 * @return string
 */
function getFailMessage() {
    global $form, $tpl;

    $tpl->assign('title', $form->getPhrase('response', 'sending_failed'));

    $tpl->assign('status', $form->getPhrase('response', 'sending_failed'));
    $tpl->assign('headline', $form->getPhrase('response', 'fail_message_headline'));
    $tpl->assign('message', $form->getPhrase('response', 'fail_message_body'));
    $tpl->assign('button', array('text' => $form->getPhrase('misc', 'new_message'), 'url' => 'index.php'));

    return $tpl->draw('sending_failed', $return = true);
}

/**
 * Validate the data (though we already do that using javascript, but it's safer this way ^^)
 */
$form->validate();

/**
 * If the data is not valid, handle the error based on the request type...
 */
if ( ! $form->isDataValid() ) {

    //  ... If the request type is ajax ...
    if ( $form->isAjaxRequest() ) {
        
        // ... The javascript validation was changed by the user, probably a hacker...  So we will mess with him :)
        // We will display a FAKE error message!
        echo getFailMessage();
        exit;

    // If not, it's a normal request. In that case, display the form with the errors
    } else {

       $tpl->assign('title', $config['home_title']);

        $tpl->assign('form_open', $form->getOpeningHTML());
        $tpl->assign('fields', $form->getFieldsWithErrorsAsArray());
        $tpl->assign('selected', $form->getPhrase('misc', 'selected'));
        $tpl->assign('form_close', $form->getClosingHTML());

        $tpl->draw('validation_failed');
        exit;

    }
}

/**
 * If we arrive at this code, then the data is valid.
 * We will generate the email message and mail it.
 */
$tpl->assign('message_title', $form->getPhrase('message', 'message_title'));
$tpl->assign('one_line_data_title', $form->getPhrase('message', 'one_line_data_title'));
$tpl->assign('multiple_lines_data_title', $form->getPhrase('message', 'multiple_lines_data_title'));
$tpl->assign('data', $form->getMessageData());
$tpl->assign('generator', 'This message was generated by <a href="http://namodg.com" style="color: #8fbacb;">Namodg</a> v' . Namodg::version . ' - all rights recieved');

/**
 * Mail the message and get the result
 */
$sentCorrectly = $form->sendUsingTemplate( $tpl->draw('email', true) );

/**
 * If something went wrong while sending, show an error message and exit
 */
if ( ! $sentCorrectly) {
    echo getFailMessage();
    exit;
}

/**
 * Everyting went fine, so we will inform the user about this.
 */
$tpl->assign('title', $form->getPhrase('response', 'sending_succeeded'));

$tpl->assign('status', $form->getPhrase('response', 'sending_succeeded'));
$tpl->assign('headline', $form->getPhrase('response', 'success_message_headline'));
$tpl->assign('message', $form->getPhrase('response', 'success_message_body'));
$tpl->assign('button', array('text' => $form->getPhrase('misc', 'new_message'), 'url' => 'index.php'));

$tpl->draw('sending_succeeded');
exit;